package mess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeeCode_face_17_05 {
    public static void main(String[] args) {
        String[] res = findLongestSubarray(new String[]{
                "A","B","1","2","C"});
        System.out.println(Arrays.toString(res));
        String[] res2 = findLongestSubarray(new String[]{
                "A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"});
        System.out.println(Arrays.toString(res2));

    }
    private static String[] findLongestSubarray(String[] array) {
        List<int[]> ress = new ArrayList<>();
        int len = array.length;
        int from = 0, to = 0, nums = 0, lts = 0;
        backTrick(array, from, to, nums, lts, ress, len);
        int max = 0;
        int[] res = new int[2];
        for(int[] Tmpres : ress){
            int count = Tmpres[1] - Tmpres[0];
            if(count > max){
                max = count;
                res[0] = Tmpres[0];
                res[1] = Tmpres[1];
            }
        }
        return Arrays.copyOfRange(array, res[0], res[1] + 1);
    }

    private static void backTrick(String[] array, int from, int to, int nums, int lts,
                                  List<int[]> ress, int len){
        if(to == len - 1 || from > to)
            return;
        if(nums == lts){
            ress.add(new int[]{from, to});
        }
        if (from == to){
            char c = array[from].charAt(0);
            if (c >= '0' && c <= '9'){
                nums = 1;
                lts = 0;
            }else {
                lts = 1;
                nums = 0;
            }
        }
        char fromC =  array[from].charAt(0);
        if(fromC >= '0' && fromC <= '9'){
           /* System.out.println(from + " " + to);
            System.out.println(nums + " " + lts);
            System.out.println("------------------");*/
            backTrick(array, from + 1, to, nums - 1, lts, ress, len);
        }else{
           /* System.out.println(from + " " + to);
            System.out.println(nums + " " + lts);
            System.out.println("------------------");*/
            backTrick(array, from + 1, to, nums, lts - 1, ress, len);
        }
        char toC = array[to + 1].charAt(0);
        if(toC >= '0' && toC <= '9'){
            /*System.out.println(from + " " + to);
            System.out.println(nums + " " + lts);
            System.out.println("------------------");*/
            backTrick(array, from, to + 1, nums + 1, lts, ress, len);
        }else{
           /* System.out.println(from + " " + to);
            System.out.println(nums + " " + lts);
            System.out.println("------------------");*/
            backTrick(array, from, to + 1, nums, lts + 1, ress, len);
        }
    }
}
